package com.mdd.common.util.sms;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import static com.mdd.common.util.sms.AliyunAppUtil.*;
public class SmsUtil {
	 public static SmsSendResult sendSms(String phoneNumber, String code) {
	        JSONObject json = new JSONObject();
	        json.put("code", code);
	        return sendSms(DEFAULT_ACCESS_KEY_ID(), DEFAULT_ACCESS_KEY_ID(), DEFAULT_SECRET(),
	                phoneNumber, DEFAULT_SIGN_NAME(), DEFAULT_TEMPLATE_NAME(), json.toJSONString());
	    }
	    
	    
	    public static SmsSendResult sendHelpSms(SmsEntity smsEntity) {
	    	return sendSms(DEFAULT_ACCESS_KEY_ID(), DEFAULT_ACCESS_KEY_ID(), DEFAULT_SECRET(),
					smsEntity.getPhoneNumber(), DEFAULT_SIGN_NAME(), smsEntity.getSmsId(), smsEntity.getJson()==null?"":smsEntity.getJson().toString());
	    }

	    public static SmsSendResult sendSms(String phoneNumber,
	                                        String signName, String templateCode, String templateParam) {
	        return sendSms(DEFAULT_ACCESS_KEY_ID(), DEFAULT_ACCESS_KEY_ID(), DEFAULT_SECRET(), phoneNumber, signName, templateCode, templateParam);
	    }

	    public static SmsSendResult sendSms(String regionId, String accessKeyId, String secret, String phoneNumber,
	                                        String signName, String templateCode, String templateParam) {
	        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
	        IAcsClient client = new DefaultAcsClient(profile);

	        CommonRequest request = new CommonRequest();
//	        request.setMethod(MethodType.POST);
	        request.setSysMethod(MethodType.POST);
//	        request.setDomain("dysmsapi.aliyuncs.com");
	        request.setSysDomain("dysmsapi.aliyuncs.com");
//	        request.setVersion("2017-05-25");
	        request.setSysVersion("2017-05-25");
//	        request.setAction("SendSms");
	        request.setSysAction("SendSms");
	        request.putQueryParameter("RegionId", "cn-hangzhou");
	        request.putQueryParameter("PhoneNumbers", phoneNumber);
	        request.putQueryParameter("SignName", signName);
	        request.putQueryParameter("TemplateCode", templateCode);
	        request.putQueryParameter("TemplateParam", templateParam);
	        try {
	            CommonResponse response = client.getCommonResponse(request);
	            System.out.println(response.getData());
	            return JSONObject.parseObject(response.getData(), SmsSendResult.class);
	        } catch (Throwable e) {
	            e.printStackTrace();
	            return new SmsSendResult().setCode(e.getMessage());
	        }
	    }


	    public static void main(String[] args) {
//	        SmsSendResult commonResponse = sendSms("18670778262", "123456");
//	        System.out.println(commonResponse.toString());
	    	SmsEntity smsEntity = new SmsEntity();
	    	smsEntity.setPhoneNumber("17347502680");
	    	smsEntity.setSmsId("SMS_219621840");
	    	JSONObject json = new JSONObject();
			json.put("name", "刘卓");
			json.put("phone", "18674841209");
			json.put("address", "世贸环球金融中心");
			smsEntity.setJson(json);
	    	sendHelpSms(smsEntity);
	    }

}
